package Messages;

import ocsf.server.ConnectionToClient;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import Logic.LoginInfo;


import server.SQLConnection;
/**
 * The purpose of this class is the create an sql query and to return subscriber current lending  
 * @author Ron Shenkerman
 * @author Gabi Revah
 * @author Aviad Almog
 * @author Ivan Onyshchenko
 */

public class MessagesearchLending {
	private ConnectionToClient client ;
	private String id,bookid,title;
	
	
	
	
	/**
	 * 
	 * @param client
	 */
	public MessagesearchLending(ConnectionToClient client) {
		super();
		this.client = client;
	}




    /**
     * send to the client subscriber current lending
     * @param str
     * @throws SQLException
     * @throws IOException
     */
	public void SearchResult(String[] str) throws SQLException, IOException {
		
		try 
		{

			System.out.println("getting book detail for viewing");
			Statement stmt,stmt1;
			SQLConnection conn = new  SQLConnection();
			stmt = conn.getConnection().createStatement();
			stmt1 = conn.getConnection().createStatement();
			
					
					
			ResultSet rs1=stmt.executeQuery("SELECT * FROM db1.Lending,db1.bookscopies  WHERE  bookscopies.CopyId=Lending.copyid AND SubscriberId='"+str[1]+"' AND Lend='"+"yes"+"';");
			ResultSet rs= stmt1.executeQuery("SELECT * FROM db1.Lending,db1.bookscopies  WHERE  bookscopies.CopyId=Lending.copyid AND SubscriberId='"+str[1]+"' AND Lend='"+"yes"+"';");		
			ArrayList<String> arr=new ArrayList<String>();
					
					
				try {
				
						if (rs1.next())
							{
							 arr.add("SearchLending");
							 while (rs.next())
							 {
								id=rs.getString(2);
								bookid=rs.getString(3);
								title=rs.getString(11);
								System.out.println("id:"+id+"  bookid:"+bookid+"  title:"+title+"");
												
							arr.add(bookid);
							arr.add(id);
							arr.add(title);
								
							 }
							client.sendToClient(arr);
								
									}
							else 
								{
								client.sendToClient("Book ID Doesn't Exsists");							
								}
  
					}
						catch (SQLException e)
								{	
								e.printStackTrace();
								}
		}
  
		catch (Exception ex )
		{
	  ex.printStackTrace();
		}
		
	}
}
